#-*- coding: UTF-8 -*-


import json
from tornado.options   import options
from stormed           import Message
from loog.mylog         import flogger as logger


METHOD_INSERT = "insert"
METHOD_UPDATE = "update"
METHOD_DELETE = "delete"


class SyncData(object):
    """ 同步数据到消息队列 """

    def __init__(self):
        if not hasattr(self,"channel"):
            raise NotImplementedError("Must configure an channel.")

    @classmethod
    def configure(cls,channel):
        cls.channel = channel

    def send_insert(self,module,className,doc):
        method = METHOD_INSERT

        doc = json.dumps(doc)
        request = Message(
            doc,
            delivery_mode = 2,
            reply_to      = options.Sync_Receive_Data_Queue,
            headers       = {
                "module"    : module,
                "className" : className,
                "method"    : method,
            }
        )
        self.channel.publish(
            request,
            exchange    = options.Sync_Send_Data_Exchange,
            routing_key = options.Sync_Send_Data_Routing_Key
        )
        logger.info("[*] sync_data insert module:%s,className:%s,doc:%s" %(module,className,doc))

    def send_update(self,module,className,spec,doc,upsert = False,safe = False, multi = False):
        method = METHOD_UPDATE

        args = json.dumps((spec,doc))

        _upsert_ = "0"
        _safe_   = "0"
        _multi_  = "0"
        if upsert:
            _upsert_ = "1"
        if safe:
            _safe_   = "1"
        if multi:
            _multi_  = "1"

        request = Message(
            args,
            delivery_mode = 2,
            reply_to      = options.Sync_Receive_Data_Queue,
            headers       = {
                "module"    : module,
                "className" : className,
                "method"    : method,
                "upsert"    : _upsert_,
                "safe"      : _safe_,
                "multi"     : _multi_
            }
        )
        self.channel.publish(
            request,
            exchange    = options.Sync_Send_Data_Exchange,
            routing_key = options.Sync_Send_Data_Routing_Key
        )
        logger.info("[*] sync_data update module:%s,className:%s,spec:%s,doc:%s,upsert:%s,safe:%s" %(module,className,spec,doc,upsert,safe))

    def send_delete(self,module,className,spec):
        method = METHOD_DELETE
        spec = json.dumps(spec)
        request = Message(
            spec,
            delivery_mode = 2,
            reply_to      = options.Sync_Receive_Data_Queue,
            headers       = {
                "module"    : module,
                "className" : className,
                "method"    : method,
            }
        )
        self.channel.publish(
            request,
            exchange    = options.Sync_Send_Data_Exchange,
            routing_key = options.Sync_Send_Data_Routing_Key
        )
        logger.info("[*] sync_data delete module:%s,className:%s,spec:%s" %(module,className,spec))


